package com.csx.springcloud.loadbalancer.scl.integration.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/")
@RestController
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class RestTemplateSample {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/testRestTemplateLoadBalancer")
    public String testRestTemplateLoadBalancer () {

        ResponseEntity<String> result = restTemplate.getForEntity("http://loadbalancer-demo:8001/test", String.class);

        String body = result.getBody();
        HttpStatusCode statusCode = result.getStatusCode();
        int statusCodeValue = result.getStatusCodeValue();
        HttpHeaders headers = result.getHeaders();

        return "restTemplate with loadbalancer invoke success";
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }

}
